var t = getApp();

Page({
    data: {
        language: "",
        show1: !1,
        inputs: "",
        goodList: [],
        chooseGood: [],
        chooseItem: [],
        lengths: 0,
        arrItms: [],
        isLoadMore: !0,
        page: 1,
        total_page: 0
    },
    onLoad: function(t) {
        this.setGoodsData(), this.getData();
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {
        this.setData({
            chooseItem: []
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {
        this.data.isLoadMore ? this.getMoreData() : console.log("没有更多了");
    },
    onShareAppMessage: function() {},
    setGoodsData: function() {
        var t = "";
        wx.getStorage({
            key: "chooseItem",
            success: function(a) {
                t = a.data;
            }
        }), t ? this.setData({
            chooseItem: t
        }) : this.setData({
            chooseItem: []
        }), this.setData({
            chooseGood: []
        });
        for (var a = this.data.chooseGood, o = 0; o < this.data.chooseItem.length; o++) a.push(this.data.chooseItem[o].id);
        this.setData({
            chooseGood: a
        }), console.log(this.data.chooseGood, "数组");
    },
    chooseGoods: function(t) {
        var a = t.target.dataset.item || t.currentTarget.dataset.item;
        if (0 == (t.target.dataset.index || t.currentTarget.dataset.index)) {
            var o = this.data.goodList, e = this.data.lengths;
            o.forEach(function(t) {
                t.goods_id == a.goods_id && (t.bler = !1, e--);
            }), this.setData({
                chooseItem: []
            }), this.setData({
                goodList: o,
                lengths: e
            });
        } else {
            var s = this.data.goodList, i = this.data.lengths;
            if (i >= 8) return void this.setData({
                show1: !0
            });
            s.forEach(function(t) {
                t.goods_id == a.goods_id && (t.bler = !0, i++);
            }), this.setData({
                goodList: s,
                lengths: i
            });
        }
        var n = [];
        0 == a.bler && n.push(a), this.setData({
            chooseItem: n
        });
    },
    checkOk: function() {
        this.setData({
            show1: !1
        });
    },
    toGood: function(t) {
        var a = t.target.dataset.id || t.currentTarget.dataset.id;
        wx.navigateTo({
            url: "/packageA/detail_v2/detail_v2?id=" + a
        });
    },
    sureChoose: function(t) {
        var a = this.data.goodList, o = 0, e = [];
        if (a.forEach(function(t) {
            if (t.bler) return o++, void e.push(t);
        }), this.setData({
            chooseItem: e
        }), 0 != o) if (o >= 8) this.setData({
            show1: !0
        }); else {
            this.setData({
                show1: !1
            });
            var s = [];
            this.data.chooseItem.forEach(function(t) {
                s.push({
                    title: t.title,
                    price: t.price,
                    thumb: t.thumb,
                    id: t.goods_id
                });
            });
            try {
                var i = wx.getStorageSync("chooseItem") || [];
                s = s.concat(i);
            } catch (t) {}
            wx.setStorage({
                key: "chooseItem",
                data: s
            }), wx.redirectTo({
                url: "/packageB/member/business_card/EditCard/EditCard?card_id=" + this.options.card_id + "&isShow=" + !0
            });
        } else wx.showToast({
            title: "请选择商品",
            icon: "none"
        });
    },
    enterSearch: function(t) {
        var a = t.detail;
        this.setData({
            inputs: a
        }), this.getData();
    },
    getData: function() {
        var a = this;
        this.setData({
            isLoadMore: !1,
            page: 1
        });
        var o = t.getNetAddresss("goods.goods.search-goods");
        o += "&search[keyword]=" + this.data.inputs + "&page=" + this.data.page + "&search[category]=" + this.options.id, 
        t._getNetWork({
            url: o,
            success: function(t) {
                var o = t.data;
                if (1 == o.result) {
                    o.data.data.forEach(function(t) {
                        t.bler = !1;
                    }), a.setData({
                        isLoadMore: !0,
                        total_page: o.data.last_page,
                        goodList: o.data.data
                    });
                    var e = a.data.goodList;
                    console.log(e, "数组的对象");
                    var s = a.data.lengths;
                    wx.getStorage({
                        key: "chooseItem",
                        success: function(t) {
                            for (var o = t.data, i = function(t) {
                                e.forEach(function(a) {
                                    a.id == o[t].id && (a.bler = !0, s++);
                                });
                            }, n = 0; n < o.length; n++) i(n);
                            a.setData({
                                goodList: e,
                                lengths: s
                            });
                        }
                    }), a.data.total_page || a.setData({
                        total_page: 0
                    });
                } else wx.showToast({
                    title: o.msg,
                    icon: "none"
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getMoreData: function() {
        var a = this;
        if (this.setData({
            isLoadMore: !1
        }), !(this.data.page >= this.data.total_page)) {
            var o = this.data.page + 1;
            this.setData({
                page: o
            });
            var e = t.getNetAddresss("goods.goods.search-goods");
            e += "&search[keyword]=" + this.data.inputs + "&page=" + this.data.page + "&search[category]=" + this.options.id, 
            t._getNetWork({
                url: e,
                success: function(t) {
                    var o = t.data;
                    if (a.setData({
                        isLoadMore: !0
                    }), 1 == o.result) {
                        var e = o.data.data.concat(a.data.goodList);
                        e.forEach(function(t) {
                            t.bler = !1;
                        });
                        var s = e;
                        wx.getStorage({
                            key: "chooseItem",
                            success: function(t) {
                                for (var o = t.data, e = function(t) {
                                    s.forEach(function(a) {
                                        a.id == o[t].id && (a.bler = !0);
                                    });
                                }, i = 0; i < o.length; i++) e(i);
                                a.setData({
                                    goodList: s
                                });
                            }
                        });
                    } else {
                        wx.showToast({
                            title: o.msg,
                            icon: "none"
                        });
                        var i = a.data.page - 1;
                        a.setData({
                            page: i,
                            isLoadMore: !1
                        });
                    }
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    }
});